/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tewi.annotations.processors;

import pl.tewi.contexts.ApplicationServerContext;
import pl.tewi.annotations.ApplicationServer;
import pl.tewi.exceptions.AnnotationProcessingException;
import pl.tewi.exceptions.GeneralFrameworkException;
import pl.tewi.exceptions.UseApplicationServerAnnotationException;

/**
 * Przetwarza adnotację {@link ApplicationServer} ustawiając nazwę domeny i 
 * typ serwera aplikacyjnego
 * @author Marcin Kwapisz
 */
public class ApplicationServerAnnotationProcessor implements AnnotationProcessor {

    /**
     * 
     */
    public ApplicationServerAnnotationProcessor() {
    }

    /**
     * Przetwarza adnotację {@link ApplicationServer}
     * @param klass klasa na której adnotacja jest przetwarzana
     * @param appServerContext kontekst serwera aplikacji (nie może być null)
     * @return uzupełniony kontekst serwera aplikacji
     * @throws AnnotationProcessingException
     * @throws GeneralFrameworkException  
     */
    @Override
    public ApplicationServerContext processAnnotations(final Class<?> klass, final ApplicationServerContext appServerContext) throws AnnotationProcessingException, GeneralFrameworkException {
        //przetwórz adnotację ApplicationServer
        ApplicationServer appServerAnnotation = klass.getAnnotation(ApplicationServer.class);
        if (appServerAnnotation != null) {
            appServerContext.setApplicationServerType(appServerAnnotation.serverType());
            appServerContext.setInstanceName(appServerAnnotation.instanceName());
            appServerContext.setInstanceParentPath(appServerAnnotation.instanceParentPath());
            appServerContext.setHostName(appServerAnnotation.hostName());
            //appServerContext.setInstancePorts(processPorts(appServerAnnotation));
            new PortAnnotationProcessor().processAnnotations(klass, appServerContext);
            new SecurityAnnotationProcessor().processAnnotations(klass, appServerContext);
            new AppResourceAnnotationProcessor().processAnnotations(klass, appServerContext);
        } else {
            throw new UseApplicationServerAnnotationException();
        }
        return appServerContext;
    }
}
